<?php

require_once dirname(__FILE__).'/../lib/videoGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/videoGeneratorHelper.class.php';

/**
 * video actions.
 *
 * @package    wills
 * @subpackage video
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class videoActions extends autoVideoActions
{
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $notice = $form->getObject()->isNew() ? '创建视频成功.' : '更新视频成功.';

      try {
        $video = $form->save();$this->resizeImages($video);//生成缩略图
      } catch (Doctrine_Validator_Exception $e) {

        $errorStack = $form->getObject()->getErrorStack();

        $message = get_class($form->getObject()) . ' has ' . count($errorStack) . " field" . (count($errorStack) > 1 ?  's' : null) . " with validation errors: ";
        foreach ($errorStack as $field => $errors) {
            $message .= "$field (" . implode(", ", $errors) . "), ";
        }
        $message = trim($message, ', ');

        $this->getUser()->setFlash('error', $message);
        return sfView::SUCCESS;
      }

      $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $video)));

      if ($request->hasParameter('_save_and_add'))
      {
        $this->getUser()->setFlash('notice', $notice.' 继续创建视频');

        $this->redirect('@video_new');
      }
      else
      {
        $this->getUser()->setFlash('notice', $notice);

        $this->redirect(array('sf_route' => 'video_edit', 'sf_subject' => $video));
      }
    }
    else
    {
      $this->getUser()->setFlash('error', '有错误，内容保存失败。', false);
    }
  }
        protected function resizeImages($object)
	 {
		$upload_dir = 'uploads/video/';
		$thumbs_dir = 'uploads/video/thumb/';
		$image = $object->getImage();
		if ($image && file_exists($upload_dir.$image))
		{
			$thumbnail = new sfThumbnail(150, 150);
			$thumbnail->loadFile($upload_dir.$image);
			$thumbnail->save($thumbs_dir.'thumb_'.$image);

			$object->setThumb('thumb_'.$image)->save();//更新thumb字段
		} 
	  }
}
